<?php
#################################
# Metadata Enhancement for Repositories (MER) 
# This software is an overlay repository search interface that uses a 
# text mined index of terms.
# Copyright (C) 2011  Rory McNicholl (University of London Computer Centre), Robert Drinkall (University College London)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Contact repositories@ulcc.ac.uk
# http://www.ulcc.ac.uk

##################################


$term = $_REQUEST["term"];
if($_REQUEST["ids"]){
	$ids_arr = split(":",$_REQUEST["ids"]);
	$hilt_ids = "true";
}else{
	$hilt_ids="false";
}

if((!$term || $term =="") && (!$ids_arr || $ids_arr=="")){
	print "<error>No term or id(s) supplied</error>";
	exit;
}else{
	$term = urlencode($term);
}
$hilt_url = "http://m2m.cdlr.strath.ac.uk:9900/hilt";
$hilt_params = array ("version"=> "1.1", "operation" => "searchRetrieve", "startRecord" => "1", "maximumRecords"=>"50");

if($hilt_ids =="false"){ //I know!

	$hilt_query = 'hilt.scheme+%3D+UNESCO+and+hilt.get_filtered_set+%3D+%22'.$term.'%22+and+hilt.preferred+%3D+true+and+hilt.related+%3D+true+and+hilt.non_preferred+%3D+true+and+hilt.is_id+%3D+'.$hilt_ids.'+and+hilt.concepts+%3D+false';

	$hilt_params["query"] = $hilt_query;

	$hilt_xml = CURL_Request($hilt_url, "GET", $hilt_params);

}else{
	$out = new DOMDocument('1.0', 'utf-8');
	$root = $out->createElementNS('http://www.loc.gov/zing/srw/', 'zs:searchRetrieveResponse');
	$out->appendChild($root);
	$root->appendChild($out->createElement("zs:version","1.1"));
	$root->appendChild($out->createElement("zs:numberOfRecords","50"));
	$root->appendChild($records = $out->createElement("zs:records"));

	foreach($ids_arr as $id){
		$records->appendChild($record = $out->createElement("zs:record"));
		$hilt_query = 'hilt.scheme+%3D+UNESCO+and+hilt.get_filtered_set+%3D+%22'.$id.'%22+and+hilt.preferred+%3D+true+and+hilt.related+%3D+true+and+hilt.non_preferred+%3D+true+and+hilt.is_id+%3D+'.$hilt_ids.'+and+hilt.concepts+%3D+false';

		$hilt_params["query"] = $hilt_query;
		$hilt_xml_part = CURL_Request($hilt_url, "GET", $hilt_params);
		
		$doc = new DOMDocument;

		// We don't want to bother with white spaces
		$doc->preserveWhiteSpace = false;
		$doc->LoadXML($hilt_xml_part);
		$xpath = new DOMXPath($doc);
		$xpath->registerNamespace('skos', "http://www.w3.org/2008/05/skos#");

		$query = '//skos:Concept';

		$concepts = $xpath->query($query);
		foreach($concepts as $concept){
			$c = $out->importNode($concept, true);
			$record->appendChild($c);
		}
	}
	$hilt_xml = $out->saveXML();
}

	header('Content-type: text/xml');
	print $hilt_xml;


function CURL_Request($url, $method="GET", $params = "") { // Remember, SSL MUST BE SUPPORTED

	if($method=="GET"){
		$par_str="?";
		foreach($params as $key=> $value){
			$par_str.="$key=$value&";
		}
		$par_str= rtrim($par_str,"&");
	}
	error_log($url . ($method == "GET" && $params != "" ? $par_str : ""));
    $curl = curl_init($url . ($method == "GET" && $params != "" ? $par_str : ""));

    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
//    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_HTTPGET, ($method == "GET"));
    curl_setopt($curl, CURLOPT_POST, ($method == "POST"));


    if ($method == "POST") curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($curl);

    if (curl_errno($curl) != 0){
      error_log(curl_error($curl));
    }
    return $response;
}

?>
